HackerRank Fraudulent Activity Notifications
https://www.hackerrank.com/challenges/fraudulent-activity-notifications/problem
提出
3/8 Terminated due to timeout :(
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
import statistics
from collections import deque
#
# Complete the 'activityNotifications' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER_ARRAY expenditure
# 2. INTEGER d
#
def activityNotifications(expenditure, d):
# Write your code here
now = deque(expenditure:d)
rest = deque(expenditured:)
ans = 0
while(len(rest)):
median = statistics.median(now)
if (rest0 >= 2*median):
ans += 1
rest_p = rest.popleft()
now.popleft()
now.append(rest_p)
return ans
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input0)
d = int(first_multiple_input1)
expenditure = list(map(int, input().rstrip().split()))
result = activityNotifications(expenditure, d)
fptr.write(str(result) + '\n')
fptr.close()
解答
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'activityNotifications' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER_ARRAY expenditure
# 2. INTEGER d
#
def activityNotifications(expenditure, d):
# Write your code here
dic = {}
# function to get median
# {2:1, 3:2, 4:1, 6:1}
# index = 3
# s = 3
def get_median(idx):
s = 0
for i in range(201):
freq = 0
if i in dic:
freq = dici
s += freq
if s >= idx:
return i
result = 0
for i in range(n):
val = expenditurei
if i >= d:
med = get_median(d//2 + d%2)
if d%2 == 0:
if val >= med + get_median(d//2 + 1):
result += 1
else:
if val >= med*2:
result += 1
# add current value in dictionary sliding window
if val not in dic:
dicval = 1
else:
dicval += 1
# remove element out of sliding window
if i >= d:
prev = expenditurei-d
dicprev -= 1
return result
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input0)
d = int(first_multiple_input1)
expenditure = list(map(int, input().rstrip().split()))
result = activityNotifications(expenditure, d)
fptr.write(str(result) + '\n')
fptr.close()
メモ
https://www.youtube.com/watch?v=qF44vXnciGQ
提出
Terminated due to timeout :(
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
from collections import deque
import statistics
#
# Complete the 'activityNotifications' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER_ARRAY expenditure
# 2. INTEGER d
#
# 2 3 4 2 3 6 8 4 5
def activityNotifications(expenditure, d):
# Write your code here
q = deque()
ans = 0
for i in range(d):
q.append(expenditurei)
for i in range(d, len(expenditure)):
# slow
# heapq? d=5 -> index=2 d=6 -> index=2,3
# median = statistics.median(q)
sq = sorted(q)
median = sqd//2 if d % 2 == 1 else (sqd//2 + sqd//2+1) / 2
if expenditurei >= 2 * median:
ans += 1
q.popleft()
q.append(expenditurei)
return ans
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input0)
d = int(first_multiple_input1)
expenditure = list(map(int, input().rstrip().split()))
result = activityNotifications(expenditure, d)
fptr.write(str(result) + '\n')
fptr.close()